<template>
  <section class="todoapp">
    <TodoHeader 
      @add-todo="fatherAddFn" />
    <TodoBody 
      :arr="showList" 
      @del-todo="fatherDelFn" />
    <TodoFooter
      v-if="list.length > 0"
      :arr="list"
      @clear-todo = 'fatherClearFn'
      @switch-todo="fatherSwitchFn"
    />
  </section>
</template>

<script>
import "./assets/css/base.css"
import "./assets/css/index.css"

import TodoHeader from './components/TodoHeader.vue'
import TodoBody from './components/TodoBody.vue'
import TodoFooter from './components/TodoFooter.vue'
export default {
  components: {
    TodoHeader,
    TodoBody,
    TodoFooter,
  },
  data () {
    return {
      fatherType : 'all',
      list: JSON.parse(window.localStorage.getItem('todo')) || [
          { id: 100, name: "吃饭", isDone: true },
          { id: 201, name: "睡觉", isDone: false },
          { id: 103, name: "打豆豆", isDone: true },
        ],
    }
  },
  computed: {
    showList(){
      if (this.fatherType === 'all') {
        return this.list
      }else if (this.fatherType === 'no') {
        return this.list.filter(item => item.isDone === false)
      }else {
        return this.list.filter(item => item.isDone === true)
      }
    }
  },
  methods: {
    fatherAddFn(name){
      const obj = {
        id: Date.now(),
        name,
        isDone: false
      } 
      this.list.push(obj)
    },
    fatherDelFn(i){
      if (window.confirm('确认删除吗？')) {
        this.list.splice(i,1)
      }
    },
    fatherSwitchFn(switchType){
      this.fatherType = switchType
    },
    fatherClearFn(){
      this.list = this.list.filter(item => item.isDone === false)
    }
  },
  watch: {
    list:{
      deep:true,
      handler(newVal){
        window.localStorage.setItem('todo',JSON.stringify(newVal))
      }
    }
  }
}
</script>

<style>
</style>